<?php

namespace common\models;

use Yii;

/**
 * 这是表 `user_rebate` 的模型
 */
class UserRebate extends \common\components\ARModel
{
    public $start_date;
    public $end_date;
    
    public function rules()
    {
        return [
            [['order_id', 'user_id', 'pid', 'point'], 'integer'],
            [['user_id', 'pid', 'amount', 'point'], 'required'],
            [['amount'], 'number'],
            [['created_at', 'updated_at'], 'safe']
        ];
    }

    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'order_id' => '订单id',
            'user_id' => '返点用户ID',
            'pid' => '经纪人用户id',
            'amount' => '返点金额',
            'point' => '返点百分比%',
            'created_at' => '申请时间',
            'updated_at' => '审核时间',
        ];
    }

    /****************************** 以下为设置关联模型的方法 ******************************/

    public function getOrder()
    {
        return $this->hasOne(Order::className(), ['id' => 'order_id']);
    }
    public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

    public function getParent()
    {
        return $this->hasOne(User::className(), ['id' => 'pid']);
    }

    public function getAdminUser()
    {
        return $this->hasOne(AdminUser::className(), ['id' => 'pid']);
    }

    /****************************** 以下为公共显示条件的方法 ******************************/

    public function search()
    {
        $this->setSearchParams();

        return self::find()
            ->filterWhere([
                'userRebate.id' => $this->id,
                'userRebate.order_id' => $this->order_id,
                'userRebate.user_id' => $this->user_id,
                'userRebate.pid' => $this->pid,
                'userRebate.amount' => $this->amount,
                'userRebate.point' => $this->point,
            ])
            ->andFilterWhere(['like', 'userRebate.created_at', $this->created_at])
            ->andFilterWhere(['like', 'userRebate.updated_at', $this->updated_at])
            ->andTableSearch()
        ;
    }

    /****************************** 以下为公共操作的方法 ******************************/

    //是否给用户返点
    public static function isUserRebate($order, $user)
    {
        if ($order->profit == 0) {
            return false;
        }
        //一级返点
        $pUser = User::find()->where(['is_manager' => User::IS_MANAGER_YES, 'id' => $user->pid])->one();
        if (!empty($pUser)) {
            //这个人是上级经纪人
            $sale = $order->fee * $pUser->point / 100;
            $pUser->account += $sale;
            $pUser->total_fee += $sale;
            $pUser->update();
            //经纪人账户表
            $userExtend = UserExtend::findModel($pUser->id);
            $userExtend->rebate_account += $sale;
            $userExtend->update();
            $userRebate = new UserRebate();
            $userRebate->user_id = $user->id;
            $userRebate->order_id = $order->id;
            $userRebate->pid = $pUser->id;
            $userRebate->amount = $sale;
            $userRebate->point = $pUser->point;
            $userRebate->insert();
        }
        //代理商返点
        if ($user->admin_id > 0) {
            $retail = Retail::findOne($user->admin_id);
            if (!empty($retail)) {
                $sale = $order->fee * $retail->point / 100;
                $retail->total_fee += $sale;
                $retail->update(false);

                $userRebate = new UserRebate();
                $userRebate->user_id = $user->id;
                $userRebate->order_id = $order->id;
                $userRebate->pid = $retail->admin_id;
                $userRebate->amount = $sale;
                $userRebate->point = $retail->point;
                $userRebate->insert();
            }
        }
    }
    

    /****************************** 以下为字段的映射方法和格式化方法 ******************************/
}
